<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends CommonController {
    public function index(){
    	/*链接数据库verify  D实例化模型*/
        $question = D('verify')->getOneVerify();

        //分配到模板 'question'可以在模板上输出验证码了
        $this->assign('question',$question);

        $this->assign('title','登陆'); //分配动态的title，对应的是'登陆'

        $this->display();
    }

    //检测推过来的数据
    public function checkData()
    {	//I方法自动转意
    	
    	//人类验证
    	$verify = I('post.verify');
    	if ($verify!= $_SESSION['answer']) {
    		$arr = array(
    			'error'=>1,
    			'info'=>'人类验证失败',
    		);
    		echo json_encode($arr);
    		exit;
    	}

    	if ($res = D('user')->checkLogin(I('post.email'),I('post.password'))){
            
            //var_dump($resstop);exit;
            //判定是否被后台停用 1已停用;0已启用
            //上面取到的res是二维数组 转成当前维 
            $resstop = current($res);
            if ($resstop['stop']==1) {
                $arr = array(
                    'error'=>1,
                    'info'=>'账号异常或已被停用,请联系管理员',
                );
                echo json_encode($arr);
                exit;
            }


    		$_SESSION['id']=current($res)['id'];
    		$_SESSION['nickname']=current($res)['nickname'];	
    		$_SESSION['face']=current($res)['face'];

    	    	$arr = array(
	    			'error'=>0,
	    			'info'=>'登陆成功',
	    		);
	    		echo json_encode($arr);
	    		exit;     	
    	   }else{
	    		$arr = array(
	    			'error'=>1,
	    			'info'=>'邮箱或密码错误',
	    		);
	    		echo json_encode($arr);
	    		exit;
	    }    	     

    }


    public function logintuichu()
    {
    	session_unset();
    	session_destroy();

    	$this->success('退出成功',U('home/index/index'),2);
    }

    //qq回调
    public function qqreturn()
    {
        $openid = getOpenid();

        $res = M('user')->where("openid='{$openid}'")->find();
        //var_dump($res);exit;
        //array (size=14)
        //  'id' => string '14' (length=2)
        //  'nickname' => string '44'
        //  'openid' => string '75ADA8330886BEE4DEA2B1248A7E0D0E'
    
        if($res){
            $_SESSION['id']=$res['id'];
            $_SESSION['nickname']=$res['nickname'];    
            $_SESSION['face']=$res['face'];

            $this->success('登陆成功',U('home/index/index'));

        }else{
            //得到qq用户的信息 qq名字 城市 年龄  图片等 
            $userarr = getUserInfo();
            //var_dump($userarr);
            /*$data = file_get_contents($userarr['figureurl_2']);

            file_put_contents('./Upload/qqface/sas.jpg',$data);*/
            $data['nickname'] = $userarr['nickname'];
            $data['openid'] = $openid;
            $data['city'] = $userarr['city'];
            $data['create_time'] = time();

            if($res = M('user')->add($data)){
                $_SESSION['id']=$res;
                $_SESSION['nickname']=$data['nickname'];    
                //$_SESSION['face']=$res['face'];

                $this->success('登陆成功',U('home/index/index'));


            }


        }

    }


    //qq登陆
    public function qqlogin()
    {
        //require_once("../../API/qqConnectAPI.php");

        HLlogin();
        //Vendor('QQConnect.API.qqConnectAPI');
        //$qc = new \QC(); // \全局类
        //var_dump($qc);exit;

        //$qc->qq_login();
    }


}

?>